/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

// File: logTest.cxx
// Author: Cory Dixon
// Date: 2/27/01
//
// Program to test the serial port both in Unix
// and VxWorks
//

#include <unistd.h>
#include <signal.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>

#include "fileLog.h"
#include "String.h"
#include "command.h"
#include "timeLib.h"
#include "type_defs.h"

// functions
void usage();		  // prints usage description

//----------------------------------------------------------
// Main function
//----------------------------------------------------------

#ifdef VXWORKS
int logTest()
#else
int main(int argc, char *argv[])
#endif
{
	String base = "log/logTest";

#ifndef VXWORKS
	// used for getopt to get otions from arguments
	extern int optind;
	//extern char * optarg;

	if( argc ==  2) {
		base = String(argv[optind]); 
	} 
#endif

	FileLog l1(base.c_str(), FileLog::CREATE);
	FileLog l2(base.c_str(), FileLog::APPEND);
	FileLog l3(base.c_str(), FileLog::INCREMENT);

	// open l1 to create the first file
	cout << "Creating log file" << endl;
	if( ! l1.open() ) {
		cout << "Could not open " << l1.getFileName() << endl;
		return ERROR;
	}
	if( l1.write("0123456789", 10) != 10)
		cout << "Did not write buffer to l1" << endl;
	if( ! l1.close() ){
		cout << "Could not close l1" << endl;
		return ERROR;
	}	
	cout << "--------------------" << endl
		<< "Bytes Written: " << l1.bytesWritten() 
		<< "\tFile Size: " << l1.fileSize() << endl;

	usleep(1);
	// open l2 to append to the first file
	cout << "Appending to log file" << endl;
	if( ! l2.open() ) {
		cout << "Could not open " << l2.getFileName() << endl;
		return ERROR;
	}
	if( l2.write("abcdefghij", 10) != 10)
		cout << "Did not write buffer to l2" << endl;
	if( ! l2.close() ){
		cout << "Could not close l2" << endl;
		return ERROR;
	}	
	cout << "--------------------" << endl
		<< "Bytes Written: " << l2.bytesWritten() 
		<< "\tFile Size: " << l2.fileSize() << endl;

	// now read back the two lines, make sure
	// it fails correctly 
	LogFileStruct lines[3];
	int linesRead = 0;
	if( ! l2.open(NULL) ) {
		cout << "Could not open " << l2.getFileName() << endl;
		return ERROR;
	}
	linesRead = l2.read( lines, 3);
	struct tm tmTime = l2.getTime();
	cout << "--------------------" << endl
		<< "File Time:"
		<< " year=" << tmTime.tm_year + 1900
		<< " month=" << tmTime.tm_mon + 1
		<< " day=" << tmTime.tm_mday 
		<< " hour=" << tmTime.tm_hour 
		<< " min=" << tmTime.tm_min 
		<< " sec=" << tmTime.tm_sec << endl
		<< "Read " << linesRead << " lines " << endl;
	for( int i=0; i < linesRead; i++)
		cout << i << ": " << setprecision(13) << setw(15) 
			<< lines[i].timeOfDay
			<< " [" << lines[i].numOfBytes << "] - " 
			<< lines[i].bytes << endl;
	l2.close();

	// open l3 to create a numbered file
	cout << "Creating numbered log file" << endl;
	if( ! l3.open() ) {
		cout << "Could not open " << l3.getFileName() << endl;
		return ERROR;
	}
	if( l3.write("abcdefghij", 10) != 10)
		cout << "Did not write buffer to l3" << endl;
	if( ! l3.close() ){
		cout << "Could not close l3" << endl;
		return ERROR;
	}	
	// open l3 to increment number file
	cout << "Incrementing numbered log file" << endl;
	if( ! l3.open() ) {
		cout << "Could not open " << l3.getFileName() << endl;
		return ERROR;
	}
	if( l3.write("abcdefghij", 10) != 10)
		cout << "Did not write buffer to l3" << endl;
	if( ! l3.close() ){
		cout << "Could not close l3" << endl;
		return ERROR;
	}	

	// exit
	return OK;
}
